package com.nbufe.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nbufe.common.security.utils.SecurityUtils;
import com.nbufe.domain.ResponseResult;
import com.nbufe.domain.dto.factor.AddFactorDto;
import com.nbufe.domain.dto.factor.EditFactorDto;
import com.nbufe.domain.entity.EmissionFactorBank;
import com.nbufe.domain.entity.EmissionOrigin;
import com.nbufe.domain.vo.PageVo;
import com.nbufe.domain.vo.factor.PageFactorBankVo;
import com.nbufe.domain.vo.factor.QueryFactorDto;
import com.nbufe.enums.AppHttpCodeEnum;
import com.nbufe.enums.StageEnum;
import com.nbufe.exception.SystemException;
import com.nbufe.mapper.EmissionFactorBankMapper;
import com.nbufe.mapper.EmissionOriginMapper;
import com.nbufe.service.DataService;
import com.nbufe.service.EmissionFactorBankService;
import com.nbufe.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 碳排放因子库(EmissionFactorBank)表服务实现类
 *
 * @author luzhiheng
 * @since 2023-12-26 16:13:08
 */
@Service("emissionFactorBankService")
public class EmissionFactorBankServiceImpl extends ServiceImpl<EmissionFactorBankMapper, EmissionFactorBank> implements EmissionFactorBankService {

    @Resource
    private EmissionOriginMapper emissionOriginMapper;

    @Resource
    private DataService dataService;

    @Override
    public ResponseResult queryAllMaterial() {
        List<EmissionOrigin> emissionOriginList = emissionOriginMapper.selectList(null);
        return ResponseResult.okResult(emissionOriginList);
    }

    @Override
    public ResponseResult pageList(QueryFactorDto dto) {
        Page<EmissionFactorBank> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<EmissionFactorBank> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(dto.getStageId() != null, EmissionFactorBank::getStage, String.valueOf(dto.getStageId()));
        queryWrapper.eq(dto.getMaterialId() != null, EmissionFactorBank::getOrganizationId, String.valueOf(dto.getMaterialId()));
        queryWrapper.orderByDesc(EmissionFactorBank::getCreateTime);
        queryWrapper.orderByDesc(EmissionFactorBank::getUpdateTime);
        this.page(page, queryWrapper);
        List<EmissionFactorBank> emissionFactorBankList = page.getRecords();
        List<PageFactorBankVo> collect = emissionFactorBankList.stream().map(emissionFactorBank -> {
            Long organizationId = emissionFactorBank.getOrganizationId();
            EmissionOrigin emissionOrigin = emissionOriginMapper.selectById(organizationId);
            PageFactorBankVo pageFactorBankVo = BeanCopyUtils.copyBean(emissionFactorBank, PageFactorBankVo.class);
            pageFactorBankVo.setName(emissionOrigin.getName());
            List<Integer> stageIdList = JSON.parseArray(emissionOrigin.getProduceStage(), Integer.class);
            StringBuilder sb = new StringBuilder();
            for (Integer stageId : stageIdList) {
                sb.append(StageEnum.getCode(stageId)).append(",");
            }
            pageFactorBankVo.setStage(sb.substring(0, sb.length() - 1));
            return pageFactorBankVo;
        }).collect(Collectors.toList());
        PageVo pageVo = new PageVo(page.getTotal(), collect);
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult deleteFactorById(Long id) {
        this.removeById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult queryDetail(Long id) {
        EmissionFactorBank emissionFactorBank = this.getById(id);
        return ResponseResult.okResult(emissionFactorBank);
    }

    @Override
    public ResponseResult deleteBatchFactor(List<Long> ids) {
        this.removeBatchByIds(ids);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult addFactor(AddFactorDto dto) {
        EmissionFactorBank emissionFactorBank = BeanCopyUtils.copyBean(dto, EmissionFactorBank.class);
        emissionFactorBank.setOrganizationId(dto.getMaterialId());
        emissionFactorBank.setFactor(dto.getEmissionFactor());
        emissionFactorBank.setCreateTime(new Date());
        emissionFactorBank.setCreateName(SecurityUtils.getUsername());
        emissionFactorBank.setDelFlag(0);

        EmissionOrigin emissionOrigin = emissionOriginMapper.selectById(dto.getMaterialId());
        emissionFactorBank.setStage(emissionOrigin.getProduceStage());

        this.save(emissionFactorBank);
        return null;
    }

    @Override
    public ResponseResult editFactor(EditFactorDto dto) {
        EmissionFactorBank emissionFactorBank = this.getById(dto.getId());
        if (emissionFactorBank == null) {
            throw new SystemException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        emissionFactorBank.setUpdateName(SecurityUtils.getUsername());
        emissionFactorBank.setUpdateTime(new Date());
        emissionFactorBank.setOrganizationId(dto.getMaterialId());
        emissionFactorBank.setFactor(dto.getFactor());
        emissionFactorBank.setDataOrigin(dto.getDataOrigin());

        EmissionOrigin emissionOrigin = emissionOriginMapper.selectById(dto.getMaterialId());
        emissionFactorBank.setStage(emissionOrigin.getProduceStage());

        this.updateById(emissionFactorBank);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult queryFactory(Long id) {
        LambdaQueryWrapper<EmissionFactorBank> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmissionFactorBank::getOrganizationId, id);
        List<EmissionFactorBank> emissionFactorBankList = this.list(queryWrapper);
        return ResponseResult.okResult(emissionFactorBankList);
    }


}

